package com.panshi.modules.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panshi.modules.domain.QaAnswer;
import com.panshi.modules.mapper.QaAnswerMapper;
import com.panshi.modules.domain.QaAnswerComment;
import com.panshi.modules.mapper.QaAnswerCommentMapper;
import com.panshi.modules.service.QaAnswerCommentService;
import com.panshi.po.CommentList;
import com.panshi.po.RevertAll;
import com.panshi.vo.QueryCommentVO;
import com.panshi.vo.SaveCommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class QaAnswerCommentServiceImpl extends ServiceImpl<QaAnswerCommentMapper, QaAnswerComment> implements QaAnswerCommentService {
    @Resource
    QaAnswerCommentMapper qaAnswerCommentMapper;
    @Resource
    QaAnswerMapper qaAnswerMapper;


    public List<RevertAll> revertAll(final QueryCommentVO queryCommentVO){
        List<QaAnswerComment> qaAnswerComments = qaAnswerCommentMapper.commentlist(queryCommentVO);
        for (QaAnswerComment item : qaAnswerComments) {
            item.setQaAnswerCommentList(qaAnswerCommentMapper.revertAll(item.getId()));
        }
        List<RevertAll> revertAll = getRevertAll(qaAnswerComments);
        return revertAll;
    }

    @Override
    public int insert(final SaveCommentVO saveCommentVO) {
        if (saveCommentVO.getUserId()==0){
            return 0;
        }
        if(saveCommentVO.getParentId()==0L){//不存在父评论
            //对应的回答表评论量进行加1
            UpdateWrapper<QaAnswer> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq("id",saveCommentVO.getAnswerId())
                    .setSql("comment_num=comment_num+1");
            //对评论等级进行存储
            saveCommentVO.setLevle(1);
            QaAnswer qaAnswer = new QaAnswer();
            qaAnswer.setUserId(saveCommentVO.getUserId());
            qaAnswerMapper.update(qaAnswer,updateWrapper);
        }else{//存在父评论
            QaAnswerComment qaAnswerComment = qaAnswerCommentMapper.selectById(saveCommentVO.getParentId());
            saveCommentVO.setLevle(qaAnswerComment.getLevle()+1);
        }
        QaAnswerComment qaAnswerComment = getQaAnswerComment(saveCommentVO);
        int insert = qaAnswerCommentMapper.insert(qaAnswerComment);
        return insert;
    }

    @Override
    public List<CommentList> commentlist(final QueryCommentVO queryCommentVO) {
        List<QaAnswerComment> list= qaAnswerCommentMapper.commentlist(queryCommentVO);
        List<CommentList> commentList = getCommentList(list);
        return commentList;
    }

    @Override
    public Integer commentCount(final QueryCommentVO queryCommentVO) {
        QueryWrapper<QaAnswerComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("answer_id",queryCommentVO.getAnswerId())
                .eq("parent_id",queryCommentVO.getParentId());
        Integer count = qaAnswerCommentMapper.selectCount(queryWrapper);
        return count;
    }

    //SaveCommentVO 转换 QaAnswerComment
    private QaAnswerComment getQaAnswerComment(SaveCommentVO saveCommentVO){
        QaAnswerComment qaAnswerComment = new QaAnswerComment();
        if (StringUtils.isEmpty(saveCommentVO)){
            return null;
        }
        BeanUtils.copyProperties(saveCommentVO,qaAnswerComment);
        return qaAnswerComment;
    }

    //CommentList 转换 QaAnswerComment
    private List<CommentList> getCommentList(List<QaAnswerComment> list){
        List<CommentList> commentList = new ArrayList<>();
        if (StringUtils.isEmpty(list)){
            return null;
        }
        for (QaAnswerComment item:list){
            CommentList comment = new CommentList();
            BeanUtils.copyProperties(item,comment);
            comment.setNickname(item.getUserMember().getNickname());
            comment.setPhotoPath(item.getUserMember().getPhotoPath());
            commentList.add(comment);
        }

        return commentList;
    }
    private List<RevertAll> getRevertAll(List<QaAnswerComment> list){
        List<RevertAll> revertAlls = new ArrayList<>();
        if (StringUtils.isEmpty(list)){
            return null;
        }
        for (QaAnswerComment item:list){
            RevertAll revertAll = new RevertAll();
            BeanUtils.copyProperties(item,revertAll);
            revertAll.setNickname(item.getUserMember().getNickname());
            revertAll.setPhotoPath(item.getUserMember().getPhotoPath());
            revertAll.setRevertAllList(getRevertAll(item.getQaAnswerCommentList()));
            revertAlls.add(revertAll);
        }
        return revertAlls;
    }
}
